package com.util;

import com.hehe.BDto;
import com.hehe.Dat;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.StringWriter;

/**
 * 作者 ：lichao
 * 部门 : 开发二部
 * 时间 ：2018/10/11
 * 公司 ：国金黄金
 */
public class TT {

    public static String beanToXml(Object obj, Class<?> load) throws JAXBException {
        JAXBContext context = JAXBContext.newInstance(load);
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "GBK");
        StringWriter writer = new StringWriter();
        marshaller.marshal(obj, writer);
        return writer.toString();

    }

    public static void main(String[] args) {

        BDto b = new BDto();
        b.setKey("1111");
        b.setClientid("123456");

        Dat dat = new Dat("11", "22", "11", "22", "11", "22", "11", "22", "11", "22", "11", "22", "11", "22", "11", "22", "11", "22", "11", "22", "11", "22", "11", "22", "11", "22", "11", "22", "11", "22", "11", "22", "11");

        Dat[] data = {dat};

        b.setData(data);

        try {
            String s = beanToXml(b, BDto.class);
            System.out.println(s);
        } catch (JAXBException e) {
            e.printStackTrace();
        }


    }
}
